// notbes.js
var app = getApp();
var util = require('../../utils/util.js');
var glo = require('../../utils/global.js');
var bibleData = require('../../utils/bible.js')
var oldbook = "";
var newbook = "";
var lastTapDiffTime = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    sorte: true, //默认正序
    opendown: false,
    notbes: [],
    onotbes: [],
    pageCount: 0,
    notbetext: decodeURI("%E7%AC%94%E8%AE%B0")
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu();
    wx.setNavigationBarTitle({
      title: '我的' + decodeURI("%E7%AC%94%E8%AE%B0"),
    })
    oldbook = bibleData.oldbooks;
    newbook = bibleData.newbooks;
    var notbesArr = new Array();
    var that = this;
    var pageCount = this.data.pageCount;
    var pageArr = [];
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo) {
      var verseArr = [];
      var notbes = userInfo.notes.sort(function(a, b) {
        return a.noteDate > b.noteDate;
      });
      console.log(notbes);
      that.data.userInfo = userInfo;

      for (var i = 0; i < notbes.length; i++) {
        var favArr = new Array();
        verseArr = notbes[i].quotes;
        that.transArrData(verseArr, favArr);
        notbes[i].indexStr = "";
        notbes[i].dateStr = new Date(parseInt(notbes[i].noteDate)).Format("yyyy-MM-dd hh:mm:ss"); //toLocaleString();
        notbes[i].contentQ = notbes[i].content;
        if (notbes[i].content.length > 50) {
          notbes[i].contentQ = notbes[i].content.substr(0, 40) + "......";
        }
        for (var j = 0; j < favArr.length; j++) {
          if (j == 0) {
            notbes[i].indexStr = favArr[j][0];
          } else {
            notbes[i].indexStr = notbes[i].indexStr + ";" + favArr[j][0];
          }
        }
      }
      for (let k = 0; k < pageCount; k++) {
        if (notbes[k] !== undefined) {
          pageArr.push(notbes[k]);
        } else {
          that.setData({
            noMore: true
          });
          break;
        }
      }
      let size = options === null ? 0 : 8;
      console.log(size)
      that.setData({
        notbes: pageArr,
        onotbes: notbes,
        pageCount: pageCount + size
      });
    });
  },
  toggledown: function() {
    var opendown = this.data.opendown;
    this.setData({
      opendown: !opendown
    });
  },
  changecure: function() {
    var sorte = this.data.sorte;
    var notbes = this.data.notbes.reverse();
    this.setData({
      sorte: !sorte,
      opendown: false,
      notbes: notbes
    });

  },
  lower: function(e) {
    if (this.data.noMore) {
      return;
    }
    let that = this,
      pageCount = this.data.pageCount,
      onotbe = this.data.onotbes,
      nArr = [];

    for (let i = 0; i < pageCount; i++) {
      if (onotbe[i] !== undefined) {
        nArr.push(onotbe[i]);
      } else {
        that.setData({
          noMore: true
        });
        break;
      }
    }
    this.setData({
      pageCount: pageCount + 8,
      notbes: nArr
    });

  },
  catalogUrl: function(e) {
    console.log(e);
    var tit = e.currentTarget.dataset.tit;
    var notbeid = e.currentTarget.dataset.notbeid;
    var notbecontent = e.currentTarget.dataset.content;
    var notbedate = e.currentTarget.dataset.notbedate;
    if (notbeid == undefined) {
      return;
    }
    var curTime = e.timeStamp;
    var lastTime = lastTapDiffTime;
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        console.log("双击事件");
        lastTapDiffTime = curTime;
        return;
      }
    }
    wx.showLoading({
      title: "正在加载...",
      mask: true
    });
    lastTapDiffTime = curTime;
    var quotes = e.currentTarget.dataset.quotes;
    var that = this;
    var verseArr = quotes; // res.data;

    var selContents = new Array();
    var selIndexs = new Array();
    var selChapters = new Array();
    glo.transArrData(verseArr, selContents, selIndexs, selChapters);
    wx.navigateTo({
      url: '../editnotbe/editnotbe?edit=1&notbeid=' + notbeid + '&content=' + JSON.stringify(selContents) + '&index=' + JSON.stringify(selIndexs) + '&chapter=' + JSON.stringify(selChapters) + "&notbedate=" + notbedate + '&tit=' + tit + "&notbecontent=" + notbecontent,
      success: function(res) {
        wx.hideLoading();
      }
    });
    
  },

  tap_edit: function(e) {
    wx.showLoading({
      title: "正在加载...",
      mask: true
    });
    var notbeid = e.target.dataset.notbeid;
    var notbecontent = e.target.dataset.content;
    if (notbeid == undefined) {
      wx.hideLoading();
      return;
    }
    if (notbecontent == undefined || notbecontent == '') {
      wx.hideLoading();
      return;
    }
    //  console.debug("notbecontent:"+notbecontent);
    var curTime = e.timeStamp;
    var lastTime = lastTapDiffTime;
    if (lastTime > 0) {
      if (curTime - lastTime < 1000) {
        console.log("双击事件");
        lastTapDiffTime = curTime;
        wx.hideLoading();
        return;
      }
    }

    lastTapDiffTime = curTime;
    var quotes = e.target.dataset.quotes;
    var that = this;
    var verseArr = quotes;
    var selContents = new Array();
    var selIndexs = new Array();
    var selChapters = new Array();
    glo.transArrData(verseArr, selContents, selIndexs, selChapters);
    wx.navigateTo({
      url: '../editnotbe/editnotbe?content=' + JSON.stringify(selContents) + '&index=' + JSON.stringify(selIndexs) + '&chapter=' + JSON.stringify(selChapters) + "&notbeid=" + notbeid + "&notbecontent=" + notbecontent + "&edit=1",
      success: function(res) {
        wx.hideLoading();
      },
      fail: function(res1) {
        wx.hideLoading();
      }
    });
   
  },
  transArrData: function(verseArr, favArr) {
    var that = this;
    var chapterArr = new Array();
    for (var i = 0; i < verseArr.length; i++) {
      var id = verseArr[i].bookId + "-" + verseArr[i].chapterId;
      if (chapterArr.indexOf(id) == -1) {
        var verIndexArr = new Array();
        chapterArr.push(id);
        for (var j = 0; j < verseArr.length; j++) {
          var id1 = verseArr[j].bookId + "-" + verseArr[j].chapterId;
          if (id1 == id) {
            verIndexArr.push(verseArr[j].verseId);
            var bookId = parseInt(verseArr[j].bookId);
            var chapterId = verseArr[j].chapterId;
            var bookName = '';
            if (bookId > 39) {
              bookName = newbook[bookId - 40].bookAddName
            } else {
              bookName = oldbook[bookId - 1].bookAddName
            }
            var selIndexs = new Array();
            var selChapters = new Array();
            that.getSelVerses(bookName, bookId, chapterId, verIndexArr, selIndexs, selChapters)
          }
        }
        for (var a0 = 0; a0 < selIndexs.length; a0++) {
          var arr0 = new Array();
          arr0[0] = selIndexs[a0];
          arr0[1] = selChapters[a0];
          favArr.push(arr0);
        }

      }
    }
  },
  getSelVerses: function(bookName, bookId, chapterId, verIndexArr, selIndexs, selChapters) {
    var minx = -1;
    var maxx = -1;
    var maxmin = new Array();
    var nn = 0;
    var index = verIndexArr.length;
    for (var i = 0; i < index; i++) {
      if (minx == -1) {
        minx = verIndexArr[i];
        maxx = verIndexArr[i];
      }
      if (i + 1 < index) {
        if (verIndexArr[i + 1] - verIndexArr[i] == 1) {
          maxx = verIndexArr[i + 1];
        } else {
          maxmin[nn] = new Array();
          maxmin[nn][0] = minx;
          maxmin[nn][1] = maxx;
          nn = nn + 1;
          minx = -1;
          maxx = -1;
        }
      } else {
        maxmin[nn] = new Array();
        maxmin[nn][0] = minx;
        maxmin[nn][1] = maxx;
      }
    }
    var bname = bookName;
    var bid = bookId;
    var cid = chapterId;
    var kn = 0;
    var selN = 0;
    for (var k = 0; k <= nn; k++) {
      var ma = maxmin[k][0];
      var mb = maxmin[k][1];
      if (ma == mb) {
        selChapters[selN] = bid + "|" + cid + ":" + ma;
        selIndexs[selN] = bname + cid + ":" + ma;
        selN = selN + 1
        kn = kn + 1;
      } else {
        var ln = mb - ma + 1;
        kn = kn + ln;
        selChapters[selN] = selChapters[selN] = bid + "|" + cid + ":" + ma + "-" + mb;
        selIndexs[selN] = bname + cid + ":" + ma + "-" + mb;
        selN = selN + 1;
      }
    }


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.onLoad(null);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {
    var v = "菜单";
    if (this.data.pageEnable) {
      v = "%E5%9C%A3%E7%BB%8F"
      v = decodeURI(v)
    }
    return {

      title: ' 这是一款免下载的小程序' + v + ',点开即可马上使用！',

      desc: '微信小程序版本' + v,

      path: '/pages/index/index'

    }
  }
})